import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import {ROLE_KEY} from "@/constants";

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
    {
        path: '/login',
        component: () => import('@/views/login/index'),
        hidden: true
    },

    {
        path: '/404',
        component: () => import('@/views/404'),
        hidden: true
    },

    // {
    //   path: '/',
    //   component: Layout,
    //   redirect: '/login',
    //   // children: [{
    //   //   path: 'dashboard',
    //   //   name: 'Dashboard',
    //   //   component: () => import('@/views/dashboard/index'),
    //   //   meta: { title: 'Dashboard', icon: 'dashboard' }
    //   // }]
    // },

    {
        path: '/meta-class-admin',
        component: Layout,
        hidden: localStorage.getItem(ROLE_KEY) !== "admin",
        children: [{
            path: '',
            name: 'meta-class-admin',
            component: () => import('@/views/meta-class-admin/index'),
            meta: {title: '类别管理', icon: 'dashboard'}
        }],
    },

    {
        path: '/enterprise-admin',
        component: Layout,
        hidden: localStorage.getItem(ROLE_KEY) !== "admin",
        children: [{
            path: '',
            name: 'enterprise-admin',
            component: () => import('@/views/enterprise-admin/index'),
            meta: {title: '企业管理', icon: 'dashboard'}
        }]
    },

    {
        path: '/bank-admin',
        component: Layout,
        hidden: localStorage.getItem(ROLE_KEY) !== "admin",
        children: [{
            path: '',
            name: 'bank-admin',
            component: () => import('@/views/bank-admin/index'),
            meta: {title: '银行管理', icon: 'dashboard'}
        }]
    },

    {
        path: '/access',
        component: Layout,
        hidden: true,
        children: [{
            path: '',
            name: 'access',
            component: () => import('@/views/access/index'),
            meta: {title: '机构赋权', icon: 'dashboard'}
        }]
    },

    {
        path: '/enterprise-info',
        component: Layout,
        hidden: localStorage.getItem(ROLE_KEY) !== "enterprise",
        children: [{
            path: '',
            name: 'enterprise-fino',
            component: () => import('@/views/enterprise-info/index'),
            meta: {title: '我司信息', icon: 'dashboard'}
        }]
    },

    {
        path: '/enterprise-detail',
        component: Layout,
        hidden: true,
        children: [{
            path: '',
            name: 'enterprise-info',
            component: () => import('@/views/enterprise-detail/index'),
            meta: {title: '公司详情', icon: 'dashboard'}
        }]
    },

    {
        path: '/my-enterprise-product',
        component: Layout,
        hidden: localStorage.getItem(ROLE_KEY) !== "enterprise",
        children: [{
            path: '',
            name: 'my-enterprise-product',
            component: () => import('@/views/my-enterprise-product/index'),
            meta: {title: '我司融资信息', icon: 'dashboard'}
        }]
    },

    {
        path: '/pub-enterprise-product',
        component: Layout,
        hidden: localStorage.getItem(ROLE_KEY) !== "enterprise",
        children: [{
            path: '',
            name: 'pub-enterprise-product',
            component: () => import('@/views/pub-enterprise-product/index'),
            meta: {title: '发布融资信息', icon: 'dashboard'}
        }]
    },

    {
        path: '/enterprise-product-detail',
        component: Layout,
        hidden: true,
        children: [{
            path: '',
            name: 'enterprise-product-detail',
            component: () => import('@/views/enterprise-product-detail/index'),
            meta: {title: '融资详情', icon: 'dashboard'}
        }]
    },

    {
        path: '/enterprise-product-admin',
        component: Layout,
        hidden: localStorage.getItem(ROLE_KEY) !== "admin",
        children: [{
            path: '',
            name: 'enterprise-product-admin',
            component: () => import('@/views/enterprise-product-admin/index'),
            meta: {title: '融资信息管理', icon: 'dashboard'}
        }]
    },


    {
        path: '/bank-product-detail',
        component: Layout,
        hidden: true,
        children: [{
            path: '',
            name: 'enterprise-product-detail',
            component: () => import('@/views/bank-product-detail/index'),
            meta: {title: '产品详情', icon: 'dashboard'}
        }]
    },

    {
        path: '/bank-product-admin',
        component: Layout,
        hidden: localStorage.getItem(ROLE_KEY) !== "admin",
        children: [{
            path: '',
            name: 'enterprise-product-admin',
            component: () => import('@/views/bank-product-admin/index'),
            meta: {title: '金融产品管理', icon: 'dashboard'}
        }]
    },

    {
        path: '/bank-product-filter',
        component: Layout,
        hidden: localStorage.getItem(ROLE_KEY) !== "enterprise",
        children: [{
            path: '',
            name: 'enterprise-product-filter',
            component: () => import('@/views/bank-product-filter/index'),
            meta: {title: '金融产品列表', icon: 'dashboard'}
        }]
    },


    //bank pages
    {
        path: '/bank-info',
        component: Layout,
        hidden: localStorage.getItem(ROLE_KEY) !== "bank",
        children: [{
            path: '',
            name: 'enterprise-product-filter',
            component: () => import('@/views/bank-info/index'),
            meta: {title: '我司信息', icon: 'dashboard'}
        }]
    },

    {
        path: '/my-bank-product',
        component: Layout,
        hidden: localStorage.getItem(ROLE_KEY) !== "bank",
        children: [{
            path: '',
            name: 'my-bank-product',
            component: () => import('@/views/my-bank-product/index'),
            meta: {title: '我司产品', icon: 'dashboard'}
        }]
    },

    {
        path: '/pub-bank-product',
        component: Layout,
        hidden: localStorage.getItem(ROLE_KEY) !== "bank",
        children: [{
            path: '',
            name: 'pub-bank-product',
            component: () => import('@/views/pub-bank-product/index'),
            meta: {title: '发布金融产品', icon: 'dashboard'}
        }]
    },

    {
        path: '/enterprise-product-filter',
        component: Layout,
        hidden: localStorage.getItem(ROLE_KEY) !== "bank",
        children: [{
            path: '',
            name: 'enterprise-product-filter',
            component: () => import('@/views/enterprise-product-filter/index'),
            meta: {title: '融资信息列表', icon: 'dashboard'}
        }]
    },

    {
        path: '/bank-detail',
        component: Layout,
        hidden: true,
        children: [{
            path: '',
            name: 'bank-detail',
            component: () => import('@/views/bank-detail/index'),
            meta: {title: '机构详情', icon: 'dashboard'}
        }]
    },

    {
        path: '/order-from-me',
        component: Layout,
        hidden: localStorage.getItem(ROLE_KEY) == 'admin',
        children: [{
            path: '',
            name: 'order-from-me',
            component: () => import('@/views/order-from-me/index'),
            meta: {title: '购入管理', icon: 'dashboard'}
        }]
    },

    {
        path: '/order-to-me',
        component: Layout,
        hidden: localStorage.getItem(ROLE_KEY) == 'admin',
        children: [{
            path: '',
            name: 'order-to-me',
            component: () => import('@/views/order-to-me/index'),
            meta: {title: '售出管理', icon: 'dashboard'}
        }]
    },

    // {
    //   path: '/upload',
    //   component: Layout,
    //   children: [{
    //     path: '',
    //     name: 'upload',
    //     component: () => import('@/views/upload/index'),
    //     meta: { title: 'upload', icon: 'dingtalk' }
    //   }]
    // },

    // 404 page must be placed at the end !!!
    {path: '*', redirect: '/404', hidden: true}
]

const createRouter = () => new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({y: 0}),
    routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router
